﻿Imports System.Text
Imports System.Security.Cryptography
Imports System.Threading
Public Class Login
    Implements ContainerInterface


    Dim IsValidated As Boolean = False

    ' TODO: insérez le code pour effectuer une authentification personnalisée à l'aide du nom d'utilisateur et du mot de passe fournis 
    ' (Voir http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' L'objet Principal personnalisé peut ensuite être associé à l'objet Principal du thread actuel comme suit : 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' CustomPrincipal est l'implémentation IPrincipal utilisée pour effectuer l'authentification. 
    ' Par la suite, My.User retournera les informations d'identité encapsulées dans l'objet CustomPrincipal
    ' telles que le nom d'utilisateur, le nom complet, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        My.Settings.Item("SQLMainConnectionString") = New CustomConnection().getConnectionString(CType(cboBD.SelectedItem, CustomConnection))

        If ValidateInfo() Then
            Me.Close()
        Else
            MsgBox("Informations de connection erroné, veuillez réessayer.")
        End If

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Application.Exit()
    End Sub


    Private Function ValidateInfo() As Boolean
        Try
            Dim oContext As New DBMainContext()
            Dim oUser As Employe
            If (From u In oContext.Employe Select u).Distinct.Count = 0 Then
                Return True
            End If

            oUser = (From u In oContext.Employe Where u.Login = UsernameTextBox.Text).FirstOrDefault
            If oUser Is Nothing Then
                Return False
            End If
            If oUser.MotDePasse = Outils.MotDePasse.GenerateHash(PasswordTextBox.Text) Or oUser.MotDePasse = PasswordTextBox.Text Then
                IsValidated = True
                Return True
            Else
                Return False
            End If

        Catch ex As SqlClient.SqlException
            If ex.ErrorCode = -2146232060 Then
                MsgBox("L'information nécessaire à la connection sur la base de donnée n'est pas valide")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Function



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConfigDB.Click
        Dim oEditConnection As New EditConnectionString

        oEditConnection.ShowDialog()
        loadConnectionList()
    End Sub

    Private Sub Login_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If Not IsValidated Then
            Application.Exit()
        End If
    End Sub

    Private Sub Login_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        loadConnectionList()


    End Sub

    Sub loadConnectionList()
        Dim oCustomConnection As New CustomConnection

        cboBD.DataSource = oCustomConnection.GetList
        cboBD.DisplayMember = "NomConnection"

    End Sub

    Public Sub AddErrorToLocalErrorProvider(ByRef oControl As System.Windows.Forms.Control, ByVal strMessage As String, Optional ByVal DispatchToActivityBox As Boolean = True) Implements ContainerInterface.AddErrorToLocalErrorProvider
        LocalErrorProvider.SetError(oControl, strMessage)
    End Sub

    Public Sub RemoveErrorToLocalErrorProvider(ByRef oControl As System.Windows.Forms.Control) Implements ContainerInterface.RemoveErrorToLocalErrorProvider
        LocalErrorProvider.SetError(oControl, Nothing)
    End Sub
End Class
